/**
 * 
 */
package org.whereiswally.job.findurl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.jppf.client.JPPFClient;
import org.jppf.client.JPPFClientConnectionImpl;
import org.jppf.client.JPPFClientConnectionStatus;
import org.jppf.client.JPPFJob;
import org.jppf.management.JMXDriverConnectionWrapper;
import org.jppf.management.JMXNodeConnectionWrapper;
import org.jppf.management.NodeManagementInfo;
import org.whereiswally.main.MainLocal;
import org.whereiswally.net.url.generation.imp.URIContinuousGenerator;
import org.whereiswally.net.url.imp.PicasaURLDao;

/**
 * @author LGIAVEDO
 * 
 */
public class FindURLRunner {

    private static JPPFClient client = null;

    public static void main(String a[]) {
	// new MainDistributed().run();
	new MainLocal().run();

	try {

	    client = new JPPFClient();
	    // initializeJmxNotifications();
	    JPPFJob job = new JPPFJob();

	    job.addTask(new FindURLJob(new PicasaURLDao(),
		    new URIContinuousGenerator("lgiavedona", "zzzzzzzzzz")));

	    client.submitNonBlocking(job.getTasks(), null, null);

	    // client.submit(job);

	} catch (Exception e) {
	    e.printStackTrace();
	} finally {

	}
    }

    public static void initializeJmxNotifications() throws Exception {
	// get a handle to the driver connection
	JPPFClientConnectionImpl c = (JPPFClientConnectionImpl) client
		.getClientConnection();
	// wait until the connection to the driver is active
	while (!c.getStatus().equals(JPPFClientConnectionStatus.ACTIVE))
	    Thread.sleep(100L);
	// query the driver's JMX server for the attached nodes
	Collection<NodeManagementInfo> nodeList = null;
	int count = 20;
	while ((count-- > 0) && (nodeList == null)) {
	    JMXDriverConnectionWrapper wrapper = c.getJmxConnection();
	    if (wrapper != null)
		nodeList = wrapper.nodesInformation();
	    if (nodeList == null)
		Thread.sleep(100L);
	}
	if (nodeList == null)
	    return;
	// establish the connection to every node
	final List<JMXNodeConnectionWrapper> jmxConnections = new ArrayList<JMXNodeConnectionWrapper>();
	for (NodeManagementInfo info : nodeList) {
	    JMXNodeConnectionWrapper jmxClient = new JMXNodeConnectionWrapper(
		    info.getHost(), info.getPort());
	    jmxClient.connect();
	    jmxConnections.add(jmxClient);
	}
	// create a task that will periodically query each node for the latest
	// task notification
	TimerTask timerTask = new TimerTask() {
	    public void run() {
		for (JMXNodeConnectionWrapper jmxClient : jmxConnections) {
		    queryTaskNotification(jmxClient);
		}
	    }
	};
	Timer timer = new Timer("JMX notifications timer");
	// schedule the task with a 1 second delay between executions
	timer.schedule(timerTask, 100L, 1000L);
    }

    /**
     * Get the latest task notification from a node.
     * 
     * @param jmxClient
     *            the JMX connection to the node.
     */
    public static void queryTaskNotification(JMXNodeConnectionWrapper jmxClient) {
	try {
	    // invoke the method "notification" on the node's MBean
	    String notif = (String) jmxClient.notification();
	    // display the resulting notification
	    System.out.println("Monitored node " + jmxClient.getId()
		    + " received notification: " + notif);
	} catch (Exception e) {
	    // ... handle the exception ...
	    System.out.println(e.getMessage());
	}
    }

}
